#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
#include<unistd.h>
#include<pthread.h>

#define MAX_N   1000000
long   arr[MAX_N];

void* gen_array1(void *arg){
    int i;
    for(i = 0;i < MAX_N/2;i++){
       arr[i] = i+1; 
    }
    pthread_exit(0);
}
void* gen_array2(void *arg){
    int i;
    for(i = MAX_N/2;i<MAX_N;i++){
        arr[i] = i+1;
    }
    pthread_exit(0);
}

int main(){
    
    struct timeval tv1,tv2;
    long sum1 = 0;
    int timelen = 0;
    gettimeofday(&tv1,NULL);
    pthread_t gen_arr1,gen_arr2;
    pthread_create(&gen_arr1,NULL,gen_array1,NULL);
    pthread_join(gen_arr1,NULL);
    pthread_create(&gen_arr2,NULL,gen_array2,NULL);
    pthread_join(gen_arr2,NULL);
    int i = 0;
    while( i < MAX_N){
        //sum2 += (j+1);
        sum1 += arr[i++];
    }
    gettimeofday(&tv2,NULL);
    timelen = tv2.tv_usec - tv1.tv_usec;
    printf("sum of array is:%lld\ntime length is:%d us\n",sum1,timelen);
}